//
//  TJTabBar.swift
//  WeiBo
//
//  Created by TanJian on 15/12/5.
//  Copyright © 2015年 Joshpell. All rights reserved.
//

import UIKit

class TJTabBar: UITabBar {

    //懒加载tabbar中间的加号按钮
    lazy var composeBtn:UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Selected)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Selected)
        
        button.addTarget(self, action: "didClickComposeBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.sizeToFit()
        
        return button
    }()
    
    func didClickComposeBtn(){
        
        print("点击了btn")
        
    }
    
//重新写tabbar必须重写initframe和initcoder方法，抛出异常
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
    func setupUI(){
        addSubview(composeBtn)
    }
    
    
    //重新布局tabbar的子控件位置
    override func layoutSubviews() {
        super.layoutSubviews()
        
        composeBtn.center = CGPoint(x:frame.width*0.5, y:frame.height*0.5)
        let margin = frame.width/5
        var index = 0
        
        
        for btn in subviews{
            
            if (btn .isKindOfClass(NSClassFromString("UITabBarButton")!)){
                
                btn.frame = CGRectMake(CGFloat(index) * margin, 0, margin, frame.height)
                
                index++
                
                if (index == 2){
                    
                    index++
                }
            }
        }
        
        
        
        
    }
    


}









